<?php
/**
 * Author: Traveler
 * Time  : 2021/9/2 11:57
 */

namespace app\service;


use app\model\IcafeModel;
use sunshine\basic\RestService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class IcafeService extends RestService
{
    /**
     * IcafeService constructor.
     */
    public function __construct()
    {
        $this->model = new IcafeModel();
        $this->initialize();
    }

    /**
     * 初始化
     */
    public function initialize()
    {
        $this->queryConditions['with'] = ['category'];
    }

    /**
     * 删除场所(重写)
     *
     * @param int        $id
     * @param array|null $relation
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function delete(int $id, ?array $relation = null): bool
    {
        // 关联删除
        $relation = ['wan', 'adsl', 'mirror', 'cnk'];
        return parent::delete($id, $relation);
    }

    /**
     * 根据省份id获取统计
     *
     * @param int $provinceId
     * @return int
     */
    public function getCountByProvince(int $provinceId): int
    {
        return $this->getCountByWhere(['province' => $provinceId]);
    }

    /**
     * 根据城市id获取统计
     *
     * @param int $cityId
     * @return int
     */
    public function getCountByCity(int $cityId): int
    {
        return $this->getCountByWhere(['city_id' => $cityId]);
    }

    /**
     * 根据区域id获取统计
     *
     * @param int $districtId
     * @return int
     */
    public function getCountByDistrict(int $districtId): int
    {
        return $this->getCountByWhere(['district_id' => $districtId]);
    }

    /**
     * 根据分类id获取统计
     *
     * @param int $categoryId
     * @return int
     */
    public function getCountByCategory(int $categoryId): int
    {
        return $this->getCountByWhere(['category_id' => $categoryId]);
    }

    /**
     * 获取技术资料
     *
     * @param $id
     * @return array
     */
    public function getTechData($id): array
    {
        $with = ['wan', 'adsl', 'mirror', 'cnk'];

        $icafe = $this->model->where('id', $id)->with($with)->findOrEmpty();
        [
            'wan'    => $wan,
            'adsl'   => $adsl,
            'mirror' => $mirror,
            'cnk'    => $cnk
        ] = $icafe;

        return compact('wan', 'adsl', 'mirror', 'cnk');
    }
}